<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          style="width: 240px"
        >
          <el-option label="未使用" value="0" />
          <el-option label="已使用" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="起止时间" prop="dateRange">
        <el-date-picker
          v-model="queryParams.dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:post:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:notice:add']"
          >生成激活码</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="noticeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" width="50" />
      <el-table-column
        label="激活码"
        align="center"
        prop="activationCode"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      />
      <el-table-column
        label="有效时间"
        align="center"
        prop="effectiveTime"
        width="180"
      />
      <el-table-column
        label="使用时间"
        align="center"
        prop="useTime"
        width="180"
      />
      <el-table-column
        label="状态"
        align="center"
        prop="statusValue"
        class-name="small-padding fixed-width"
      >
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageSize"
      v-model:limit="queryParams.pageCount"
      @pagination="getList"
    />

    <!-- 添加 -->
    <el-dialog :title="title" v-model="open" width="560px" append-to-body>
      <el-form
        ref="systemMessageRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="有效期" prop="effectiveTimeType">
              <el-radio-group v-model="form.effectiveTimeType" class="ml-4">
                <el-radio :label="0" size="large">10分钟</el-radio>
                <el-radio :label="1" size="large">30分钟</el-radio>
                <el-radio :label="2" size="large">60分钟</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="生成数量" prop="createNum">
              <el-input-number v-model="form.createNum" :min="1" :max="10" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SystemMessage">
import {
  list,
  add,
  detail,
  del,
  bacthDel
} from "@/api/device/code";

const { proxy } = getCurrentInstance();
const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const detailVisible = ref(false);
const data = reactive({
  form: {
    effectiveTimeType: 0,
    createNum: 10,
  },
  queryParams: {
    dateRange:[],
    pageCount: 10,
    pageSize: 1,
    status: "",
  },
  rules: {
    effectiveTimeType: [
      { required: true, message: "请选择有效期", trigger: "blur" },
    ],
    createNum: [{ required: true, message: "请输入生成数量", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询消息列表 */
function getList() {
  loading.value = true;
  list({
    ...queryParams.value,
    startTime: queryParams.value.dateRange.length > 0 ? queryParams.value.dateRange[0] : "",
    endTime: queryParams.value.dateRange.length > 0 ? queryParams.value.dateRange[1] : "",
  }).then((response) => {
    noticeList.value = response.data.records;
    total.value = response.data.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("/device/manage/excel/export", {
    ...queryParams.value,
    ids:ids.value,
    startTime: queryParams.value.dateRange.length > 0 ? queryParams.value.dateRange[0] : "",
    endTime: queryParams.value.dateRange.length > 0 ? queryParams.value.dateRange[1] : ""
  });
}
/** 表单重置 */
function reset() {
  form.value = {
    effectiveTimeType: 0,
    createNum: 10,
  };
  proxy.resetForm("systemMessageRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageSize = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "发布消息";
}
/**详情 */
function handleDetail(row) {
  const ids = row.id || ids.value;
  detail(ids).then((response) => {
    form.value = response.data;
    detailVisible.value = true;
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["systemMessageRef"].validate((valid) => {
    if (valid) {
      add(form.value).then((response) => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const ids = row.id;
  proxy.$modal
    .confirm('是否确认删除消息编号为"' + ids + '"的数据项？')
    .then(function () {
      return del(ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 批量删除 */
function handleBatchDelete() {
  const data = ids.value;
  proxy.$modal
    .confirm('是否确认删除消息编号为"' + data.join(",") + '"的数据项？')
    .then(function () {
      return bacthDel({ ids: data });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>

<style scoped lang="scss">
.batchDel {
  margin-top: 24px;
}
</style>
